<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ModelNumber extends BaseModel
{
    use HasFactory;
    protected $table = 'model_number';
    // 允许批量操作的字段
    protected $fillable = ['model_number','is_del','file_id','created_at','updated_at'];

    public function scopeSearch($query,$params)
    {
        if(!empty($params['model_number'])){
            $query->where('model_number','like','%'.$params['model_number'].'%');
        }
        $query->where('is_del',$params['is_del']??2);


        if(isset($params['field_list']) && !empty($params['field_list'])){
            $select_arr = explode(',',$params['field_list']);
        }else{
            $select_arr = "*";
        }
        $query->select($select_arr);

        return $query;
    }

    //规格
    public function specification(){
        return $this->belongsToMany(Specification::class,'model_number_specification','model_number_id','specification_id')
            ->withPivot('unit_price');
    }

    //产品
    public function product(){
        return $this->belongsToMany(Product::class,'product_model_number','model_number_id','product_id');
    }

    public function jobTypePrice(){
        return $this->belongsToMany(ProductJobType::class,'product_center','model_number_id','pid');
    }

    public function file(){
        return $this->hasOne(File::class,'id','file_id');
    }

    public function model_number_model(){
        return $this->hasMany(ModelNumberColor::class,'model_number_id','id');
    }
    public function model_number_color(){
        return $this->belongsToMany(Color::class,'model_number_color','model_number_id','semi_modelid');
    }
}
